<?php

use yii\helpers\Html;
use yii\grid\GridView;
use common\models\fenhuicat\Fenhuicat;

/* @var $this yii\web\View */
/* @var $searchModel common\models\fenhuicat\FenhuicatSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = Yii::t('app', '分会栏目管理');
$this->params['breadcrumbs'][] = $this->title;
?>
<?php $this->beginBlock('siderbar'); ?>
<?= $this->render('//layouts/fenhui-menu') ?>
<?php $this->endBlock(); ?>


<script type="text/javascript" src="<?= yii::getAlias('@web');?>/js/jquery-2.2.3.min.js"></script>
<div class="term-index">
    <p>
        <?= Html::a(Yii::t('app', 'Create'), ['create'], ['class' => 'btn btn-success']) ?>

    <div id="w0" class="grid-view"><div class="summary">第<b>1-<?=$num?></b>条，共<b><?=$num?></b>条数据.</div>
        <table class="table table-striped table-bordered" id="content_list">
            <thead>
            <tr>
                <th>ID</th>
                <th><a href="<?= yii\helpers\Url::to(['fenhuicat/index/','sort'=>'name']);?>" data-sort="name">栏目</a></th>
                <th><a href="<?= yii\helpers\Url::to(['fenhuicat/index/','sort'=>'title']);?>" data-sort="title">分会名称</a></th>
                <th><a href="<?= yii\helpers\Url::to(['fenhuicat/index/','sort'=>'status']);?>" data-sort="status">导航显示</a></th>
                <th width="200">操作</th>
            </tr>
            </thead>
            <tbody>
            <style>
                .bianse td{
                    background-color:#f9fafc!important;
                }

                </style>
            <tr >
                <td style="width:20%"><input class="form-control" name="FenhuigradeSearch[id]" value="" type="text"></td>
                <td style="width:20%"><input class="form-control" name="FenhuigradeSearch[id]" value="" type="text"></td>
                <td style="width:20%">
                    <select id="fgid" class="form-control" name="FenhuigradeSearch[fg_id]"  style="display:block" >
                        <option value="0">全部</option>
                        <?php foreach ($grade as $gg){ ?>
                        <option value="<?=$gg['id']?>" <?php if($fg_id == $gg['id']){ ?> selected <?php } ?> ><?=$gg['name']?></option>
                        <?php } ?>
                    </select>

                </td>
                <td style="width:20%"><input class="form-control" name="FenhuigradeSearch[id]" value="" type="text"></td>
                <td style="width:20%"><span class="btn btn-primary" style="margin-left: 20px">搜索栏</span></td>
            </tr>
            <?php foreach($data as $key => $val) : ?>

                <tr data-key="<?=$val['id']?>" id=<?php echo 'cat_'.$val['id'];?>>
                    <td style="width:20%"><?=$val['id']?></td>

                    <td>
                        <a href="javascript:;" class="cat_btn" data-url="<?= yii\helpers\Url::to(['fenhuicat/children/']);?>/<?=$val['id']?>?layer=1">+</a> <?=$val['name']?>
                    </td>
                    <td style="width:20%"><?=$val['title']?></td>

                    <td><span class="label <?php if($val['status']==1){echo 'label-success';}elseif($val['status']==0){echo 'label-warning';}elseif($val['status']==-1){echo 'label-danger';}?>"><?php if($val['status']==1){echo '是';}elseif($val['status']==0){echo '否';}elseif($val['status']==-1){echo '删除';}?></span></td>

                    <td>

                        <a href="<?= yii\helpers\Url::to(['fenhuicat/view/','id'=>$val['id']]);?>" title="查看">&nbsp;&nbsp;<span class="glyphicon glyphicon-eye-open" >查看</span></a>

                        <a href="<?= yii\helpers\Url::to(['fenhuicat/update/','id'=>$val['id']]);?>" title="修改">&nbsp;&nbsp;<span class="glyphicon glyphicon-pencil">修改</span></a>

                        <?php if(fenhuicat::isDel($val['id'])) $data = "该栏目下有子栏目或者有内容,不能删除！"; else $data = "你确定要删除该栏目吗？"; ?>
                        <a href="<?= yii\helpers\Url::to(['fenhuicat/delete/','id'=>$val['id']]);?>" title="删除" data-confirm="<?=$data?>" data-method="post">&nbsp;&nbsp;<span class="glyphicon glyphicon-trash" >删除</span></a></td>


                </tr>

            <?php endforeach;?>

            </tbody></table>
    </div>
    <script type="text/javascript">
        $(function(){
            //分类展开与收起
            $('#content_list').delegate('.cat_btn', 'click',function(){
                var tr_val = '';
                var tr_str = '';
                var tr_id = $(this).closest('tr').attr('id');
                var id = $(this).closest('tr').attr('data-key');
                if($(this).hasClass('cat_down')) {
                    $(this).removeClass('cat_down').html('+');
                    $.getJSON("<?= yii\helpers\Url::to(['fenhuicat/getchildren/']);?>/"+id,function(data){
                        $.each(data, function(i, item){
                            $('.cat_'+item.parent_id+'_children').remove();
                        });
                    });
                    $('.loading').remove();
                } else {
                    $(this).addClass('cat_down').html('--');
                    var url = $(this).attr('data-url');
                    $('#'+tr_id).after('<tr class="tb_list loading"><td>loading...</td></tr>');
                    var active_id = tr_id;
                    $.getJSON(url,function(data){
                        $('.loading').remove();
                        $.each(data, function(i, item){
                            var space = '';
                            for(i=1;i<item.layer;i++) {
                                space += '&nbsp;&nbsp;&nbsp;&nbsp;';
                            }
                            if(item.status==1){
                                tr_val = 'label-success';
                                tr_str = '是';
                            }else if(item.status==0){
                                tr_val = 'label-warning';
                                tr_str = '否';
                            }else if(item.status==-1){
                                tr_val = 'label-danger';
                                tr_str = '删除';
                            }
                            $showhas = "-";
                            if(item.haschildren >0 ){
                                $showhas = "+"
                            }
                            $showcontent = "";
                            if(item.haschildren ==0 ){
                                $showcontent = '<a href="<?= yii\helpers\Url::to(['articles/contentindex/']);?>/'+item.id+'" title="查看列表">&nbsp;&nbsp;<span class="glyphicon glyphicon-pencil">查看列表</span></a>'
                            }

                            var html = '<tr data-key="'+item.id+'" id=cat_'+item.id+' class="cat_children tb_list cat_'+item.parent_id+'_children">'
                                + '<td>'+space+'├──'+item.id+'</td>'
                                + '<td>'+space+'<a href="javascript:;" class="cat_btn" data-url="<?= yii\helpers\Url::to(['fenhuicat/children/']);?>/'+item.id+'?layer='+item.layer+'" >'+$showhas+'</a>'+ item.name+'</td>'
                                + '<td>'+item.fg_id+'</td>'
                                + '<td>'+'<span class="label '+tr_val+'">'+tr_str+'</span></td>'
                                + '<td>'
                                + '<a href="'+item.view_url+'" title="查看">&nbsp;&nbsp;<span class="glyphicon glyphicon-eye-open" >查看</span></a><a href="'+item.update_url+'" title="修改">&nbsp;&nbsp;<span class="glyphicon glyphicon-pencil">修改</span></a><a href="'+item.delete_url+'" title="删除" data-confirm="'+item.deltrue+'" data-method="post">&nbsp;&nbsp;<span class="glyphicon glyphicon-trash" >删除</span></a>'+$showcontent
                                + '</td>'
                                + '</tr>';
                            $('#'+active_id).after(html);
                            active_id = 'cat_'+item.id;
                        });
                    });
                }
            });
        });
    </script>
    <script>
        $(document).ready(function() {
            $("#fgid").change(function(){
                var fgid = $('#fgid option:selected') .val();
                window.location.href="<?= yii\helpers\Url::to(['fenhuicat/fg/']);?>/"+fgid;
            });
        });
    </script>
</div>
